package com.michaelbulava.TFS.tasks;//package com.michaelbulava.TFS.tasks;
//
//import com.intellij.openapi.diagnostic.Logger;
//import com.intellij.openapi.progress.ProgressIndicator;
//import com.intellij.openapi.progress.Task;
//import com.intellij.openapi.project.Project;
//import com.intellij.openapi.ui.DialogWrapper;
//import com.michaelbulava.TFS.Messages;
//import com.michaelbulava.TFS.log.FileLogger;
//import com.michaelbulava.TFS.ui.commands.ConnectCommand;
//import com.michaelbulava.TFS.ui.dialogs.connect.CredentialsDialog;
//import com.michaelbulava.TFS.ui.helpers.UIHelpers;
//import com.microsoft.tfs.core.TFSConnection;
//import com.michaelbulava.TFS.persistence.IdeaPersistenceStoreProvider;
//import com.microsoft.tfs.core.credentials.CachedCredentials;
//import com.microsoft.tfs.core.credentials.CredentialsManagerFactory;
//import com.microsoft.tfs.core.exceptions.ACSUnauthorizedException;
//import com.microsoft.tfs.core.exceptions.TFSUnauthorizedException;
//import com.microsoft.tfs.core.httpclient.Credentials;
//import com.microsoft.tfs.core.httpclient.DefaultNTCredentials;
//import com.microsoft.tfs.core.util.CredentialsUtils;
//import com.microsoft.tfs.core.ws.runtime.exceptions.TransportRequestHandlerCanceledException;
//
//import java.net.URI;
//
///**
// * Created by mbulava on 5/6/2015.
// */
//public abstract class ConnectTask extends Task{
//    private static final Logger log = FileLogger.getInstance(ConnectTask.class);
//
//    private final URI serverURI;
//    private Credentials credentials;
//    private TFSConnection connection;
//    private boolean showErrorDialog = true;
//
//    public ConnectTask(Project project, URI serverURI) {
//        this(project, serverURI, null);
//    }
//
//    public ConnectTask(Project project, URI serverURI, Credentials credentials){
//        super(project, Messages.getString("ConnectTask.Title"), true);
//        this.serverURI = serverURI;
//        this.credentials = credentials;
//    }
//
//    @Override
//    public void run(ProgressIndicator indicator) {
//        if (this.credentials == null){
//            CachedCredentials cachedCredentials = CredentialsManagerFactory
//                    .getCredentialsManager(DefaultPersistenceStoreProvider.INSTANCE).getCredentials(serverURI);
//
//            this.credentials = (cachedCredentials != null) ? cachedCredentials.toCredentials() :
//                    new DefaultNTCredentials();
//        }
//        if (credentials == null || CredentialsUtils.needsPassword(this.credentials)){
//            CredentialsDialog credentialsDialog = new CredentialsDialog(myProject, this.serverURI);
//            credentialsDialog.setCredentials(this.credentials);
//            //credentialsDialog.setAllowSavePassword(CredentialsManagerFactory.getCredentialsManager(DefaultPersistenceStoreProvider.INSTANCE).canWrite());
//            if (UIHelpers.openOnUIThread(credentialsDialog) != DialogWrapper.OK_EXIT_CODE)
//                indicator.cancel();
//            else
//                this.credentials = credentialsDialog.getCredentials();
//        }
//        while (this.connection == null){
//            ConnectCommand connectCommand = getConnectCommand(serverURI, credentials);
//            try{
//                indicator.setText2(connectCommand.getName());
//                connectCommand.run(indicator);
//                if (!indicator.isCanceled())
//                    this.connection = connectCommand.getConnection();
//            } catch(RuntimeException e){
//                if (e instanceof TFSUnauthorizedException || e instanceof ACSUnauthorizedException){
//                    CredentialsDialog credentialsDialog = new CredentialsDialog(myProject, this.serverURI);
//                    credentialsDialog.setErrorMessage(e.getLocalizedMessage());
//                    credentialsDialog.setCredentials(this.credentials);
//                    //credentialsDialog.setAllowSavePassword(CredentialsManagerFactory.getCredentialsManager(DefaultPersistenceStoreProvider.INSTANCE).canWrite());
//                    if (UIHelpers.openOnUIThread(credentialsDialog) == DialogWrapper.OK_EXIT_CODE){
//                        this.credentials = credentialsDialog.getCredentials();
//                        continue;
//                    }
//                } else if (!(e instanceof TransportRequestHandlerCanceledException)){
//                    log.warn("Unexpected connection exception", e);
//                    final RuntimeException re = e;
//                    if (this.showErrorDialog){
//                        UIHelpers.runOnUIThread(false, new Runnable() {
//                            @Override
//                            public void run() {
//                                com.intellij.openapi.ui.Messages.showErrorDialog(re.getLocalizedMessage(),
//                                        Messages.getString("TeamProjectSelectControl.ConnectionFailedDialogTitle"));
//                            }
//                        });
//                    }
//                }
//            } catch (Exception e) {
//                log.warn("Unexpected connection exception", e);
//                final Exception re = e;
//                if (this.showErrorDialog){
//                    UIHelpers.runOnUIThread(false, new Runnable() {
//                        @Override
//                        public void run() {
//                            com.intellij.openapi.ui.Messages.showErrorDialog(re.getLocalizedMessage(),
//                                    Messages.getString("TeamProjectSelectControl.ConnectionFailedDialogTitle"));
//                        }
//                    });
//                }
//            }
//            indicator.stop();
//        }
//    }
//
//    protected abstract ConnectCommand getConnectCommand(URI serverURI, Credentials credentials);
//
//
//}
